package com.example.biji.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.biji.dao.NoteDao;
import com.example.biji.model.Note;
import com.example.biji.R;

public class NoteDetailActivity extends AppCompatActivity {
    private EditText etTitle, etContent;
    private Button btnSave, btnDelete;
    private NoteDao noteDao;
    private int noteId, userId;
    private Note note;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_note_detail);

        noteId = getIntent().getIntExtra("noteId", -1);
        userId = getIntent().getIntExtra("userId", -1);

        if (noteId == -1 || userId == -1) {
            Toast.makeText(this, "数据获取失败", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }

        initViews();
        loadNote();
        setupListeners();
    }

    private void initViews() {
        etTitle = findViewById(R.id.et_note_title);
        etContent = findViewById(R.id.et_note_content);
        btnSave = findViewById(R.id.btn_save_note);
        btnDelete = findViewById(R.id.btn_delete_note);
        noteDao = new NoteDao(this);
    }

    private void loadNote() {
        note = noteDao.queryNoteById(noteId);
        if (note == null) {
            Toast.makeText(this, "笔记不存在", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }

        etTitle.setText(note.getTitle());
        etContent.setText(note.getContent());
    }

    private void setupListeners() {
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateNote();
            }
        });

        btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteNote();
            }
        });
    }

    private void updateNote() {
        String title = etTitle.getText().toString().trim();
        String content = etContent.getText().toString().trim();

        if (title.isEmpty()) {
            Toast.makeText(this, "标题不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        note.setTitle(title);
        note.setContent(content);
        note.setUpdateTime(new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date()));

        int result = noteDao.updateNote(note);
        if (result > 0) {
            Toast.makeText(this, "更新成功", Toast.LENGTH_SHORT).show();
            finish();
        } else {
            Toast.makeText(this, "更新失败，请重试", Toast.LENGTH_SHORT).show();
        }
    }

    private void deleteNote() {
        int result = noteDao.deleteNote(noteId);
        if (result > 0) {
            Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
            finish();
        } else {
            Toast.makeText(this, "删除失败，请重试", Toast.LENGTH_SHORT).show();
        }
    }
}